//
//  MerchandisePictureAndPriceViewController.m
//  
//
//  Created by yechu on 15/11/3.
//
//

#import "MerchandisePictureAndPriceViewController.h"

/**
 *  左右滑动切换图片。
 */
@interface MerchandisePictureAndPriceViewController ()<UIScrollViewDelegate>

@property (strong, nonatomic) IBOutlet UIScrollView *pictureScrollView;


/**
 * 存储图片名称的数组,数组元素的个数，当前显示图片的数组下标。
 */
@property (strong,nonatomic) NSMutableArray *pictureArray;
@property (assign,nonatomic) NSInteger pictureCount;
@property (assign,nonatomic) NSInteger currentImageIndex;

/**
 *  显示商品图片的UIImageView
 */
@property (strong,nonatomic) UIImageView *centerPicture;
@property (strong,nonatomic) UIImageView *leftPicture;
@property (strong,nonatomic) UIImageView *rightPicture;

/**
 *  当前屏幕的宽和高。
 */
@property (assign,nonatomic) CGFloat screenWidth;
@property (assign,nonatomic) CGFloat screenHeight;

@end

@implementation MerchandisePictureAndPriceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //设置图片数组的数据
    _pictureArray = [NSMutableArray arrayWithObjects:@"0.png", @"1.png", @"2.png", @"3.png", @"4.png", nil];
    
    _screenHeight = [UIScreen mainScreen].bounds.size.height;
    _screenWidth = [UIScreen mainScreen].bounds.size.width;
    _pictureCount = _pictureArray.count;
    
    self.automaticallyAdjustsScrollViewInsets = false;

    [self setScrollViewAttributes];
    [self addImageView];
    [self setDefaultImage];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *  设置ScrollView的属性。
 */
- (void)setScrollViewAttributes {
    //设置ScrollView的滑动内容大小。
    _pictureScrollView.contentSize = CGSizeMake(_pictureCount * _screenWidth, _screenHeight);
    //最初设置当前显示的中间的图片。
    [_pictureScrollView setContentOffset:CGPointMake(_screenWidth, 0) animated:NO];
    //设置分页。
    _pictureScrollView.pagingEnabled = YES;
    //去掉水平滑动滚动条。
    _pictureScrollView.showsHorizontalScrollIndicator = NO;
}

/**
 *  添加显示图片的三个UIImageView
 */
- (void)addImageView {
    _leftPicture = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _screenWidth, _screenHeight)];
    _leftPicture.contentMode = UIViewContentModeScaleToFill;
    
    _centerPicture = [[UIImageView alloc] initWithFrame:CGRectMake(_screenWidth, 0, _screenWidth, _screenHeight)];
    _centerPicture.contentMode = UIViewContentModeScaleToFill;
    
    _rightPicture = [[UIImageView alloc] initWithFrame:CGRectMake(_screenWidth * 2, 0, _screenWidth, _screenHeight)];
    _centerPicture.contentMode = UIViewContentModeScaleToFill;
    
    //将控件添加到ScrollView中
    [_pictureScrollView addSubview:_leftPicture];
    [_pictureScrollView addSubview:_centerPicture];
    [_pictureScrollView addSubview:_rightPicture];
}

/**
 *  设置默认显示的图片
 */
- (void)setDefaultImage {
    _leftPicture.image = [UIImage imageNamed:[NSString stringWithFormat:@"%li.png",_pictureCount - 1]];
    _centerPicture.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.png", 0]];
    _rightPicture.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.png", 1]];
    _currentImageIndex = 0;
}

/**
 *  重新加载显示的图片。
 */
- (void)reloadImage {
    NSInteger leftImageIndex;
    NSInteger rightImageIndex;
    CGPoint offSet = _pictureScrollView.contentOffset;
    
    //判断滑动方向,并设置即将要显示的图片下标。
    if (offSet.x > _screenWidth) {
        //向右滑动
        _currentImageIndex = (_currentImageIndex + 1) % _pictureCount;
    }
    else {
        if (offSet.x < _screenWidth) {
            //向左滑动
            _currentImageIndex = (_currentImageIndex + _pictureCount - 1) % _pictureCount;
        }
    }
    
    //设置左右图片的下标。
    leftImageIndex = (_currentImageIndex + _pictureCount - 1) % _pictureCount;
    rightImageIndex = (_currentImageIndex + 1) % _pictureCount;
    
    //设置图片
    _leftPicture.image = [UIImage imageNamed:[NSString stringWithFormat:@"%li", (long)leftImageIndex]];
    _centerPicture.image = [UIImage imageNamed:[NSString stringWithFormat:@"%li.png", (long)_currentImageIndex]];
    _rightPicture.image = [UIImage imageNamed:[NSString stringWithFormat:@"%li.png", (long)rightImageIndex]];
}

#pragma mark - UIScrollViewDelegate

/**
 *  滑动减速事件
 *
 *  @param scrollView
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //重新加载图片
    [self reloadImage];
    //移动到中间
    [_pictureScrollView setContentOffset:CGPointMake(_screenWidth, 0) animated:NO];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
